
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>编写视图 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="视图装饰器" href="decorators.html" />
    <link rel="prev" title="URL调度器" href="urls.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="urls.html" title="URL调度器">previous</a>
     |
    <a href="../index.html" title="使用 Django" accesskey="U">up</a>
   |
    <a href="decorators.html" title="视图装饰器">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-http-views">
            
  <div class="section" id="s-writing-views">
<span id="writing-views"></span><h1>编写视图<a class="headerlink" href="#writing-views" title="永久链接至标题">¶</a></h1>
<p>一个视图函数（或简称为视图）是一个 Python 函数，它接受 Web 请求并返回一个 Web 响应。这个响应可以是 Web 页面的 HTML 内容，或者重定向，或者404错误，或者 XML 文档，或一个图片...或是任何内容。视图本身包含返回响应所需的任何逻辑。这个代码可以存在任何地方，只要它在你的 Python 路径上就行。可以说，不需要其他东西，这里并没有魔法。为了将代码放置在某处，约定将视图放在名为 <code class="docutils literal notranslate"><span class="pre">views.py</span></code> 的文件里，这个文件放置在项目或应用目录里。</p>
<div class="section" id="s-a-simple-view">
<span id="a-simple-view"></span><h2>一个简单的视图<a class="headerlink" href="#a-simple-view" title="永久链接至标题">¶</a></h2>
<p>这里是一个以 HTML 文档形式返回当前日期和时间的视图：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>
<span class="kn">import</span> <span class="nn">datetime</span>

<span class="k">def</span> <span class="nf">current_datetime</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
    <span class="n">html</span> <span class="o">=</span> <span class="s2">&quot;&lt;html&gt;&lt;body&gt;It is now </span><span class="si">%s</span><span class="s2">.&lt;/body&gt;&lt;/html&gt;&quot;</span> <span class="o">%</span> <span class="n">now</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>
</pre></div>
</div>
<p>让我们来完成这个代码：</p>
<ul>
<li><p class="first">首先，我们从 <a class="reference internal" href="../../ref/request-response.html#module-django.http" title="django.http: Classes dealing with HTTP requests and responses."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.http</span></code></a> 模块导入类 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> ，以及 Python 的 <code class="docutils literal notranslate"><span class="pre">datetime</span></code> 库。</p>
</li>
<li><p class="first">然后，我们定义一个名为 <code class="docutils literal notranslate"><span class="pre">current_datetime</span></code> 的函数。这是一个视图函数。每个视图函数都将 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpRequest</span></code></a> 对象作为第一个参数，通常名为 <code class="docutils literal notranslate"><span class="pre">request</span></code> 。</p>
<p>注意视图函数名称无关紧要；它不需要以特定的名称来让 Django 识别它。我们在这里命名 <code class="docutils literal notranslate"><span class="pre">current_datetime</span></code> ，因为这个名字可以清楚的表示它的用途。</p>
</li>
<li><p class="first">视图返回一个包含生成的响应的 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> 对象。每个视图函数都要返回 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> 对象。（有例外，我们稍后再讲）</p>
</li>
</ul>
<div class="admonition-django-s-time-zone admonition">
<p class="first admonition-title">Django 时区</p>
<p class="last">Django 包含 <a class="reference internal" href="../../ref/settings.html#std:setting-TIME_ZONE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TIME_ZONE</span></code></a> 设置，默认是 <code class="docutils literal notranslate"><span class="pre">America/Chicago</span></code> 。你可以在配置文件里改成你所在的时区。</p>
</div>
</div>
<div class="section" id="s-mapping-urls-to-views">
<span id="mapping-urls-to-views"></span><h2>将 URL 映射到视图<a class="headerlink" href="#mapping-urls-to-views" title="永久链接至标题">¶</a></h2>
<p>因此，回顾一下，这个视图函数返回包含当前日期时间的HTML页面。如果在特定的 URL 使用这个视图，你需要创建 <em>URLconf</em> ；查看 <a class="reference internal" href="urls.html"><span class="doc">URL调度器</span></a> 的操作说明。</p>
</div>
<div class="section" id="s-returning-errors">
<span id="returning-errors"></span><h2>返回错误信息<a class="headerlink" href="#returning-errors" title="永久链接至标题">¶</a></h2>
<p>Django 提供了有关返回 HTTP 错误代码的帮助。<a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> 的子类除了200外，还有很多常见的 HTTP 状态代码。你可以在 <a class="reference internal" href="../../ref/request-response.html#ref-httpresponse-subclasses"><span class="std std-ref">request/response</span></a> 文档中找到所有可用子类的列表。返回这些子类中某个子类的实例而不是 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> 来表示错误。比如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span><span class="p">,</span> <span class="n">HttpResponseNotFound</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="k">if</span> <span class="n">foo</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">HttpResponseNotFound</span><span class="p">(</span><span class="s1">&#39;&lt;h1&gt;Page not found&lt;/h1&gt;&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s1">&#39;&lt;h1&gt;Page was found&lt;/h1&gt;&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>并不是每个可用 HTTP 响应代码都有专门指定的子类，因为它们很多并不常见。然而，如 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> 文档中所述的那样，你也可以将 HTTP 状态代码传递给 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> 的构造函数，这样就可以为任何状态代码创建返回类。比如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c1"># ...</span>

    <span class="c1"># Return a &quot;created&quot; (201) response code.</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="mi">201</span><span class="p">)</span>
</pre></div>
</div>
<p>因为 404 错误是最常见的 HTTP 错误，这里有更简单的方法来处理这些错误。</p>
<div class="section" id="s-the-http404-exception">
<span id="the-http404-exception"></span><h3><code class="docutils literal notranslate"><span class="pre">Http404</span></code> 异常<a class="headerlink" href="#the-http404-exception" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.http.Http404">
<em class="property">class </em><code class="descclassname">django.http.</code><code class="descname">Http404</code><a class="headerlink" href="#django.http.Http404" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>当你返回错误，例如 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponseNotFound" title="django.http.HttpResponseNotFound"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponseNotFound</span></code></a> ，你需要定义错误页面的 HTML 。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="n">HttpResponseNotFound</span><span class="p">(</span><span class="s1">&#39;&lt;h1&gt;Page not found&lt;/h1&gt;&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>为方便起见，在你的网站里有个一致的 404 错误页面是个好办法，Django 提供 <code class="docutils literal notranslate"><span class="pre">Http404</span></code> 异常。如果你在视图的任何地方引发了 <code class="docutils literal notranslate"><span class="pre">Http404</span></code> ，Django 会捕捉到它并且返回标准的错误页面，连同 HTTP 错误代码 404 。</p>
<p>用法示例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">Http404</span>
<span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>
<span class="kn">from</span> <span class="nn">polls.models</span> <span class="kn">import</span> <span class="n">Poll</span>

<span class="k">def</span> <span class="nf">detail</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">Poll</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">Poll</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="s2">&quot;Poll does not exist&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;polls/detail.html&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;poll&#39;</span><span class="p">:</span> <span class="n">p</span><span class="p">})</span>
</pre></div>
</div>
<p>为了在 Django 返回404时显示自定义的 HTML，你可以创建名为  <code class="docutils literal notranslate"><span class="pre">404.html</span></code> 的HTML模板，并将其放置在你的模板树顶层。这个模板将在 <a class="reference internal" href="../../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 设为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 时提供。</p>
<p>当 <a class="reference internal" href="../../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 时，你可以提供 <code class="docutils literal notranslate"><span class="pre">Http404</span></code> 信息，并且在标准的 404 调试模板里显示。使用这些信息来调试；它们通常不适合在生产环境下的404模板。</p>
</div>
</div>
<div class="section" id="s-customizing-error-views">
<span id="s-id1"></span><span id="customizing-error-views"></span><span id="id1"></span><h2>自定义报错视图<a class="headerlink" href="#customizing-error-views" title="永久链接至标题">¶</a></h2>
<p>Django 里默认的报错视图应该能满足大部分的 Web 应用，但你也可以很方便的自定义。指定处理程序，如下方所示。（在其他地方配置它不会有任何效果）。</p>
<p>可以用 <a class="reference internal" href="../../ref/urls.html#django.conf.urls.handler404" title="django.conf.urls.handler404"><code class="xref py py-data docutils literal notranslate"><span class="pre">handler404</span></code></a>: 覆盖 <a class="reference internal" href="../../ref/views.html#django.views.defaults.page_not_found" title="django.views.defaults.page_not_found"><code class="xref py py-func docutils literal notranslate"><span class="pre">page_not_found()</span></code></a> 视图：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">handler404</span> <span class="o">=</span> <span class="s1">&#39;mysite.views.my_custom_page_not_found_view&#39;</span>
</pre></div>
</div>
<p>可以用 <a class="reference internal" href="../../ref/urls.html#django.conf.urls.handler500" title="django.conf.urls.handler500"><code class="xref py py-data docutils literal notranslate"><span class="pre">handler500</span></code></a>: 覆盖 <a class="reference internal" href="../../ref/views.html#django.views.defaults.server_error" title="django.views.defaults.server_error"><code class="xref py py-func docutils literal notranslate"><span class="pre">server_error()</span></code></a> 视图：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">handler500</span> <span class="o">=</span> <span class="s1">&#39;mysite.views.my_custom_error_view&#39;</span>
</pre></div>
</div>
<p>可以用 <a class="reference internal" href="../../ref/urls.html#django.conf.urls.handler403" title="django.conf.urls.handler403"><code class="xref py py-data docutils literal notranslate"><span class="pre">handler403</span></code></a>: 覆盖 <a class="reference internal" href="../../ref/views.html#django.views.defaults.permission_denied" title="django.views.defaults.permission_denied"><code class="xref py py-func docutils literal notranslate"><span class="pre">permission_denied()</span></code></a> 视图：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">handler403</span> <span class="o">=</span> <span class="s1">&#39;mysite.views.my_custom_permission_denied_view&#39;</span>
</pre></div>
</div>
<p>可以用 <a class="reference internal" href="../../ref/urls.html#django.conf.urls.handler400" title="django.conf.urls.handler400"><code class="xref py py-data docutils literal notranslate"><span class="pre">handler400</span></code></a>: 覆盖 <a class="reference internal" href="../../ref/views.html#django.views.defaults.bad_request" title="django.views.defaults.bad_request"><code class="xref py py-func docutils literal notranslate"><span class="pre">bad_request()</span></code></a> 视图：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">handler400</span> <span class="o">=</span> <span class="s1">&#39;mysite.views.my_custom_bad_request_view&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<p class="last">使用 <a class="reference internal" href="../../ref/settings.html#std:setting-CSRF_FAILURE_VIEW"><code class="xref std std-setting docutils literal notranslate"><span class="pre">CSRF_FAILURE_VIEW</span></code></a> 来覆盖 CSRF 报错视图。</p>
</div>
<div class="section" id="s-testing-custom-error-views">
<span id="testing-custom-error-views"></span><h3>测试自定义报错视图<a class="headerlink" href="#testing-custom-error-views" title="永久链接至标题">¶</a></h3>
<p>为了测试自定义报错处理的响应，可以适当地在测试视图里引发异常。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="kn">import</span> <span class="n">PermissionDenied</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>
<span class="kn">from</span> <span class="nn">django.test</span> <span class="kn">import</span> <span class="n">SimpleTestCase</span><span class="p">,</span> <span class="n">override_settings</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>


<span class="k">def</span> <span class="nf">response_error_handler</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">exception</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s1">&#39;Error handler content&#39;</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="mi">403</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">permission_denied_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">raise</span> <span class="n">PermissionDenied</span>


<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;403/&#39;</span><span class="p">,</span> <span class="n">permission_denied_view</span><span class="p">),</span>
<span class="p">]</span>

<span class="n">handler403</span> <span class="o">=</span> <span class="n">response_error_handler</span>


<span class="c1"># ROOT_URLCONF must specify the module that contains handler403 = ...</span>
<span class="nd">@override_settings</span><span class="p">(</span><span class="n">ROOT_URLCONF</span><span class="o">=</span><span class="vm">__name__</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">CustomErrorHandlerTests</span><span class="p">(</span><span class="n">SimpleTestCase</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">test_handler_renders_template_response</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;/403/&#39;</span><span class="p">)</span>
        <span class="c1"># Make assertions on the response here. For example:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertContains</span><span class="p">(</span><span class="n">response</span><span class="p">,</span> <span class="s1">&#39;Error handler content&#39;</span><span class="p">,</span> <span class="n">status_code</span><span class="o">=</span><span class="mi">403</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-async-views">
<span id="s-id2"></span><span id="async-views"></span><span id="id2"></span><h2>异步视图<a class="headerlink" href="#async-views" title="永久链接至标题">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 3.1.</span> </div>
<p>除了同步函数，视图也可以是异步（“async”）函数，通常使用 Python 的 <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code> 语法定义。Django 会自动检测这些函数，并在异步上下文中运行它们。但是，你需要使用基于 ASGI 的异步服务器来获得它们的性能优势。</p>
<p>下面是一个异步视图的例子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>

<span class="k">async</span> <span class="k">def</span> <span class="nf">current_datetime</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
    <span class="n">html</span> <span class="o">=</span> <span class="s1">&#39;&lt;html&gt;&lt;body&gt;It is now </span><span class="si">%s</span><span class="s1">.&lt;/body&gt;&lt;/html&gt;&#39;</span> <span class="o">%</span> <span class="n">now</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>
</pre></div>
</div>
<p>你可以在 <a class="reference internal" href="../async.html"><span class="doc">异步支持</span></a> 中阅读更多关于 Django 的异步支持，以及如何最好的使用异步视图。</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">编写视图</a><ul>
<li><a class="reference internal" href="#a-simple-view">一个简单的视图</a></li>
<li><a class="reference internal" href="#mapping-urls-to-views">将 URL 映射到视图</a></li>
<li><a class="reference internal" href="#returning-errors">返回错误信息</a><ul>
<li><a class="reference internal" href="#the-http404-exception"><code class="docutils literal notranslate"><span class="pre">Http404</span></code> 异常</a></li>
</ul>
</li>
<li><a class="reference internal" href="#customizing-error-views">自定义报错视图</a><ul>
<li><a class="reference internal" href="#testing-custom-error-views">测试自定义报错视图</a></li>
</ul>
</li>
<li><a class="reference internal" href="#async-views">异步视图</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="urls.html"
                        title="上一章">URL调度器</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="decorators.html"
                        title="下一章">视图装饰器</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/topics/http/views.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="urls.html" title="URL调度器">previous</a>
     |
    <a href="../index.html" title="使用 Django" accesskey="U">up</a>
   |
    <a href="decorators.html" title="视图装饰器">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>